package mythread.demo002;

import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.JdkFutureAdapters;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListenableFutureTask;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;

public class TestListenableFuture2 {
    // 创建线程池
    final static ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
    final static ExecutorService exservice = Executors.newCachedThreadPool();

    public static void main(String[] args) throws Exception {
        Long t1 = System.currentTimeMillis();
//        // 任务1
//        ListenableFuture<Boolean> booleanTask = service.submit(new Callable<Boolean>() {
//            @Override
//            public Boolean call() throws Exception {
//                return true;
//            }
//        });
//
//        Futures.addCallback(booleanTask, new FutureCallback<Boolean>() {
//            @Override
//            public void onSuccess(Boolean result) {
//                System.err.println("BooleanTask: " + result);
//            }
//
//            @Override
//            public void onFailure(Throwable t) {
//            }
//        });
//
        // 任务2
        ListenableFuture<String> stringTask = service.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                return "Hello World";
            }
        });

        Futures.addCallback(stringTask, new FutureCallback<String>() {
            @Override
            public void onSuccess(String result) {
                System.err.println("StringTask: " + result);
            }

            @Override
            public void onFailure(Throwable t) {
            }
        });

//        // 任务3
//        ListenableFuture<Integer> integerTask = service.submit(new Callable<Integer>() {
//            @Override
//            public Integer call() throws Exception {
//                return new Random().nextInt(100);
//            }
//        });
//
//        Futures.addCallback(integerTask, new FutureCallback<Integer>() {
//            @Override
//            public void onSuccess(Integer result) {
//                try {
//                    Thread.sleep(500);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                System.err.println("IntegerTask: " + result);
//            }
//
//            @Override
//            public void onFailure(Throwable t) {
//            }
//        });

        // 执行时间
        System.err.println("time: " + (System.currentTimeMillis() - t1));
        
      
        
        // 任务4
        Future<String> strTask = exservice.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                return "ppppp";
            }
        });
        
        ListenableFuture<String> stringTask2 =JdkFutureAdapters.listenInPoolThread(strTask);
        Futures.addCallback(stringTask2, new FutureCallback<String>() {
            @Override
            public void onSuccess(String result) {
                System.err.println("stringTask2: " + result);
            }

            @Override
            public void onFailure(Throwable t) {
            }
        });
        
//        FutureTask<String> str33 = (FutureTask<String>) exservice.submit(new Callable<String>() {
//            @Override
//            public String call() throws Exception {
//                return "ppppp";
//            }
//        });
        
        ListenableFuture<String> stringTask3 =ListenableFutureTask.create(new Callable<String>() {
			@Override
			public String call() throws Exception {
				return "uuuuuuuu";
			}
 		}) ;
        
        Futures.addCallback(stringTask3, new FutureCallback<String>() {
            @Override
            public void onSuccess(String result) {
                System.err.println("stringTask3: " + result);
            }

            @Override
            public void onFailure(Throwable t) {
            }
        });
        
        
//        ListenableFuture<String> stringTask4 =ListenableFutureTask.create(new Runnable() {
//			@Override
//			public void run() {
//				
//			}
//		}, "dddddddd");
//        Futures.addCallback(stringTask4, new FutureCallback<String>() {
//            @Override
//            public void onSuccess(String result) {
//                System.err.println("stringTask4: " + result);
//            }
//
//            @Override
//            public void onFailure(Throwable t) {
//            }
//        });
        
//        ListenableFuture stringTask5= Futures.transform(input, function, executor)
//        ListenableFuture stringTask6= Futures.transform(input, function)

        
//        Futures.allAsList(futures)
    }

}